package controller

import (
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/wonderivan/logger"
	"net/http"
	"teresa/service"
)

var Configmap configmap

type configmap struct{}

func (cm *configmap) GetConfigMaps(ctx *gin.Context) {
	params := new(struct {
		Namespace  string `json:"namespace"`
		FilterName string `json:"filtername"`
		Limit      int    `json:"limit"`
		Page       int    `json:"page"`
	})
	if err := ctx.ShouldBindJSON(params); err != nil {
		logger.Error(errors.New("Bind绑定参数失败, " + err.Error()))
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  errors.New("Bind绑定参数失败, " + err.Error()),
			"data": nil,
		})
		return
	}
	dataList, err := service.Configmap.GetConfigMaps(params.Namespace, params.FilterName, params.Limit, params.Page)
	if err != nil {
		logger.Error(err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "获取configmap资源列表失败",
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取configmap资源列表成功",
		"data": dataList,
	})
}

func (cm *configmap) GetConfigMapDetail(ctx *gin.Context) {
	params := new(struct {
		Namespace     string `form:"namespace"`
		configmapname string `form:"configmapname"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error(errors.New("Bind绑定参数失败, " + err.Error()))
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  errors.New("Bind绑定参数失败, " + err.Error()),
			"data": nil,
		})
		return
	}
	datadetail, err := service.Configmap.GetConfigMapDetail(params.Namespace, params.configmapname)
	if err != nil {
		logger.Error(err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "获取configmap详情失败",
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "获取configmap详情成功",
		"data": datadetail,
	})
}

func (cm *configmap) DeleteConfigMap(ctx *gin.Context) {
	params := new(struct {
		Namespace     string `form:"namespace"`
		configmapname string `form:"configmapname"`
	})
	if err := ctx.Bind(params); err != nil {
		logger.Error(errors.New("Bind绑定参数失败, " + err.Error()))
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  errors.New("Bind绑定参数失败, " + err.Error()),
			"data": nil,
		})
		return
	}
	err := service.Configmap.DeleteConfigMap(params.Namespace, params.configmapname)
	if err != nil {
		logger.Error(err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  "删除configmap失败",
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "删除configmap成功",
		"data": nil,
	})
}

func (cm *configmap) UpdateConfigMap(ctx *gin.Context) {
	params := new(struct {
		Namespace string `json:"namespace"`
		Content   string `json:"content"`
	})
	if err := ctx.ShouldBindJSON(params); err != nil {
		logger.Error(errors.New("bind绑定失败," + err.Error()))
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  errors.New("bind绑定失败," + err.Error()),
			"data": nil,
		})
		return
	}
	err := service.Configmap.UpdateConfigMap(params.Namespace, params.Content)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg":  "更新configmap成功",
		"data": nil,
	})
}
